<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    function __construct() {
        parent::__construct();
         if ($this->session->userdata('login') == FALSE) {
            redirect('login');
        }
    }

    function index() {
       
        $this->load->model('menu_model'); 
        $this->load->model('user_model'); 
        
        $users = $this->user_model->_loadAll();
        
        $data = array();
        $data['current'] = 'User';
        $data['link'] = 'user';
        $data['menus'] = $this->menu_model->_load();
        $data['users'] = $users;
        
        $this->load->view('users-view',$data);
    }
    
     function view($username) {
		
        $this->load->model('menu_model');
        $this->load->model('user_model');
        
        $data = array();
        $data['current'] = 'User';
        $data['link'] = 'user';
        $data['menus'] = $this->menu_model->_load();        
        $data['role'] = $this->user_model->_loadRole();
        $data['user'] = $this->user_model->_getByUsername($username);
      
        //print_r($data['user']);die;
        $this->load->view('users-detail-view',$data);        
    }
    
    
    function add() {
        $this->load->model('menu_model');
        $this->load->model('user_model');

        $data = array();
        $data['current'] = 'User';
        $data['link'] = 'user';
        $data['menus'] = $this->menu_model->_load();
        $data['role'] = $this->user_model->_loadRole();
        $data['avaible'] = $this->uri->segment(3);
        $this->load->view('users-add-view',$data);        
    }
    
   
      
    function processInsert() {
        
        $this->load->model('user_model');
        $redirect = 'user';
        
        if(!$this->user_model->_checkUsername($this->input->post('username',true))) { $redirect = 'user/add/usernot'; }
        if(!$this->user_model->_checkEmail($this->input->post('email',true))) { $redirect = 'user/add/emailnot'; }
        if($this->user_model->_checkEmail($this->input->post('email',true)) == FALSE && $this->user_model->_checkUsername($this->input->post('username',true)) == FALSE) { $redirect = 'user/add/bothnot'; }
        
        
        if($this->user_model->_checkUsername($this->input->post('username',true)) == TRUE && $this->user_model->_checkEmail($this->input->post('email',true)) == TRUE) {
        $data = array();
        $data['username'] = $this->input->post('username',true);
        $data['id_role'] = $this->input->post('id_role',true);  
        $data['password'] = $this->_encode_password($this->input->post('password'));  
        $data['email'] = $this->input->post('email',true);  
        $data['about'] = $this->input->post('about',true);  
        $data['insert_date'] = date("Y-m-d H:i:s");   
        
        $file_name = $data['username'].'-'.rand(1,9999); 
        
        $path_thumbnail = $_FILES['path_image']['name'];
        if(!empty($path_thumbnail)) {
        $data['path_image'] = $this->user_model->uploadImage($file_name);        
        }
        
        $this->db->insert('m_user',$data);
        }
        
         redirect($redirect);
    }
      
    function processUpdate($username) {
        
        $this->load->model('user_model');
                
        $data = array();
        $data['id_role'] = $this->input->post('id_role');  
        
        $password = $this->input->post('password');
        
        if(!empty($password)) {
        $data['password'] = $this->_encode_password($password); }  
        
        $data['about'] = $this->input->post('about',true);  
        
        $file_name = $username.'-'.rand(1,9999); 
        
        $path_thumbnail = $_FILES['path_image']['name'];
        if(!empty($path_thumbnail)) {
        $data['path_image'] = $this->user_model->uploadImage($file_name);        
        }
        
        $this->db->update('m_user',$data,'username = "'.$username.'"');
        
        
         redirect('user');
    }
    
    function processDelete($username) {
        $this->db->delete('m_user',"username = '$username'");
         redirect('user');
    }
    
    private function _encode_password($string)
    {
        $string .= $this->config->item('encryption_key');
	// Return the SHA-1 encryption
        return sha1($string);
    }
    
      
}
?>